package bs

import net.liftweb._
import http._
import util._
import Helpers._
import code.lib.Tablable

case class Table[T: Tablable](data: T*) extends LiftComponent {
  override val template = Templates("templates-hidden" :: "bs" :: "_table" :: Nil).get
  val styles = collection.mutable.Buffer.empty[String]

  override def transformer = {
    import code.lib.ExtCssSel._
    val tablable = implicitly[Tablable[T]]

    "caption *" #> tablable.caption &
    "thead" #> {
      "th" #> tablable.columns.map("th *" #> _)
    } &
    "tbody" #> {
      "tr" #> data.map(item =>
        "td" #> tablable.asRow(item)
      )
    } &? (styles.nonEmpty) -> ".table [class+]" #> styles.mkString(" ")
  }

  def striped = {
    styles += "table-striped"
    this
  }

  def bordered = {
    styles += "table-bordered"
    this
  }

  def hover = {
    styles += "table-hover"
    this
  }

  def condensed = {
    styles += "table-condensed"
    this
  }
}
